package com.ambow.servlet;



import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.Entity.*;
import com.ambow.service.*;
import com.ambow.service.impl.*;


@WebServlet("/PaymentServlet")
public class PaymentServlet extends HttpServlet {
       SupplierService supplierService = new SupplierServiceImpl();
       StockReceiveService sReceiveService = new StockReceiveServiceImpl();
       PaymentService paymentService = new PaymentServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
		if(status.equals("querySupply")) {
			querySupply(request,response);//根据供应商拿到对应的入库信息
		}else if(status.equals("pay")) {
			pay(request,response);//报销
		}else if(status.equals("query")) {
			query(request, response);//报销查询
		}
		
	}
	
	//报销查询
	private void query(HttpServletRequest request, HttpServletResponse response) {
		try {
			String supplyName = request.getParameter("supplyName");//供应商名
		    String flag = request.getParameter("flag");
		
			Supplier supplier =supplierService.queryone("from Supplier where name=?",supplyName);
			if(supplier!=null) {
				Integer supplier_id = supplier.getId();
				List<StockReceive> sReceives = sReceiveService.queryByHql("from StockReceive where supplier_id="+supplier_id);
				if(sReceives!=null&&!sReceives.isEmpty()) {
					System.out.println(sReceives.size()+"hhhhhhhhhhh");
					List total = new ArrayList();
					List<Payment> payments = new ArrayList<Payment>();
					for(int i=0;i<sReceives.size();i++) {
						Payment payment = new Payment();
						String hql = null;
						if(flag.equals("未报销")) {
							hql = "from Payment where status='未报销' and stockReceive_id="+sReceives.get(i).getId();
						}else if(flag.equals("已报销")){
							hql="from Payment where status='已报销' and stockReceive_id="+sReceives.get(i).getId();
						}else {
							hql ="from Payment where stockReceive_id="+sReceives.get(i).getId();
						}
						List<Payment> pList = paymentService.queryByProperty(hql);
						if(pList!=null&&!pList.isEmpty()) {
							payment=pList.get(0);
							payments.add(payment);
						}
					}
					if(payments!=null&&!payments.isEmpty()) {
						request.setAttribute("payments", payments);
						request.getRequestDispatcher("pages/module/system/system001/order_list_name2.jsp").forward(request, response);
					}else {
						response.sendRedirect("pages/module/system/system001/order_list_name2.jsp");
					}
				}else {
					response.sendRedirect("pages/module/system/system001/order_list_name2.jsp");
				}
			}else {
				response.sendRedirect("pages/module/system/system001/order_list_name2.jsp");
			}
			} catch (Exception e) {
			}
		
	}
	//报销
	private void pay(HttpServletRequest request, HttpServletResponse response) {
		try {
		Integer payment_id = Integer.valueOf(request.getParameter("receive_id"));//报销id
		Payment payment = paymentService.queryById(Payment.class,payment_id);
		payment.setPayDate(new Date());
		payment.setStatus("已报销");
		boolean b = paymentService.update(payment);
		if(b) {
			System.out.println("报销成功");
			request.getRequestDispatcher("pages/module/system/system001/order_list_name2.jsp").forward(request, response);
		}else {
			System.out.println("报销失败");
			response.sendRedirect("pages/module/system/system001/order_list_name.jsp");
		}
		} catch (Exception e) {
		}
		
	}
	//根据供应商拿到对应的入库信息
	private void querySupply(HttpServletRequest request, HttpServletResponse response) {
		try {
		String supplyName = request.getParameter("supplyName");//供应商名
		if(supplyName=="") {
			supplyName=null;
		}
		System.out.println("suppyname:::::::"+supplyName);
			Supplier supplier = supplierService.queryone("from Supplier as supplier where supplier.name=?",supplyName);
			System.out.println("1234567890-"+supplier.getId());
		if(supplier!=null) {
			Integer supplier_id = supplier.getId();
			List<StockReceive> sReceives = sReceiveService.queryByHql("from StockReceive where supplier_id="+supplier_id);
			if(sReceives!=null&&!sReceives.isEmpty()) {
				System.out.println(sReceives.size()+"hhhhhhhhhhh");
				List total = new ArrayList();
				List<Payment> payments = new ArrayList<Payment>();
				for(int i=0;i<sReceives.size();i++) {
					Payment payment = new Payment();
					List<Payment> pList = paymentService.queryByProperty("from Payment where status='未报销' and stockReceive_id="+sReceives.get(i).getId());
					if(pList!=null&&!pList.isEmpty()) {
						payment=pList.get(0);
						payments.add(payment);
					}
					System.out.println(payments.size()+"ddddddddd");
				}
				if(payments!=null&&!payments.isEmpty()) {
					for(int j=0;j<payments.size();j++) {
						Integer amount = payments.get(j).getStockReceive().getAmount();
						Double price = payments.get(j).getStockReceive().getTextBook().getPrice();
						total.add(amount*price);
					}
					request.setAttribute("total", total);
					request.setAttribute("payments", payments);
					request.getRequestDispatcher("pages/module/system/system001/order_list_name.jsp").forward(request, response);
				}else {
					response.sendRedirect("pages/module/system/system001/order_list_name.jsp");
				}
			}else {
				response.sendRedirect("pages/module/system/system001/order_list_name.jsp");
			}
		}else {
			response.sendRedirect("pages/module/system/system001/order_list_name.jsp");
		}
		} catch (Exception e) {
		}
	}

}
